package com.aliqin.mytel;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.util.Log;

import com.mobile.auth.gatewayauth.PhoneNumberAuthHelper;
import com.mobile.auth.gatewayauth.TokenResultListener;
import com.mobile.auth.gatewayauth.model.InitResult;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.concurrent.CountDownLatch;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
 * Instrumented test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    private PhoneNumberAuthHelper alicomAuthHelper;
    private TokenResultListener mTokenListener;
    private CountDownLatch signal;
    private boolean getTokenRet;
    public static final String TAG = "AuthSDK ExampleInstrumentedTest";

    @Test
    public void useAppContext() throws Exception {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getTargetContext();

        assertEquals("alicom.com.authdemo", appContext.getPackageName());
    }

    @Before
    public void init(){
        signal = new CountDownLatch(1);
        final Context appContext = InstrumentationRegistry.getTargetContext();
        mTokenListener = new TokenResultListener() {

            @Override
            public void onTokenSuccess(final String ret) {
                Log.e(TAG, "TokenResultListener onTokenSuccess: " + ret);
                getTokenRet = true;
                signal.countDown();  //这里countDown，外面的await()才能结束

            }

            @Override
            public void onTokenFailed(final String ret) {
                Log.e(TAG, "TokenResultListener onTokenFail: " + ret);
                getTokenRet = false;
                signal.countDown();  //这里countDown，外面的await()才能结束
            }
        };

        alicomAuthHelper = PhoneNumberAuthHelper.getInstance(appContext, mTokenListener);
        alicomAuthHelper.setDebugMode(true);

    }

    @Test
    public void testInit(){
        InitResult ret = alicomAuthHelper.init();
        assertEquals(true, ret.isCan4GAuth());
    }

    @Test
    public void testCMCCGetToken() throws InterruptedException {
        InitResult ret = alicomAuthHelper.init();
        if (ret != null && ret.isCan4GAuth()){
            //测试移动
            alicomAuthHelper.getAuthToken(3000);
        }
        signal.await();
        assertTrue(getTokenRet);
    }

    @Test
    public void testCUCCGetToken() throws InterruptedException {
        InitResult ret = alicomAuthHelper.init();
        if (ret != null && ret.isCan4GAuth()){
            //测试联通
            alicomAuthHelper.getAuthToken(3000);
        }
        signal.await();
        assertTrue(getTokenRet);
    }

    @Test
    public void testCTCCGetToken() throws InterruptedException {
        InitResult ret = alicomAuthHelper.init();
        if (ret != null && ret.isCan4GAuth()){
            //测试电信
            alicomAuthHelper.getAuthToken(3000);
        }
        signal.await();
        assertTrue(getTokenRet);
    }

    @After
    public void destory(){
        alicomAuthHelper.onDestroy();
    }
}
